//
//  SDTagViewItem.m
//  SDEditTagsView
//
//  Created by shansander on 2017/7/29.
//  Copyright © 2017年 shansander. All rights reserved.
//

#import "SDTagViewItem.h"

@implementation SDTagViewItem

- (instancetype)initWithTag:(NSString * )tag
{
    self = [super init];
    if (self) {
        self.tag_string = tag;
        
        self.backgroundColor = [UIColor colorWithRed:246.f / 255.f green:246.f/ 255.f blue:1 alpha:1];
        
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 3.f;
    }
    return self;
}

- (void)setTag_string:(NSString *)tag_string
{
    _tag_string = tag_string;
    self.tagLabel.text = _tag_string;
}

- (CGSize)getTagSize
{
    CGSize tag_size = [self.tagLabel.text sizeWithAttributes:@{NSFontAttributeName:self.tagLabel.font}];
    return CGSizeMake(tag_size.width + 10, tag_size.height + 5);
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGSize tag_size = [self.tagLabel.text sizeWithAttributes:@{NSFontAttributeName:self.tagLabel.font}];

    self.frame = (CGRect){self.frame.origin,{tag_size.width + 10,tag_size.height + 5}};
    self.tagLabel.frame = (CGRect){{self.bounds.size.width/2.f - tag_size.width / 2.f, self.bounds.size.height / 2.f - tag_size.height / 2.f},tag_size};
}

- (void)setSelected:(BOOL)selected
{
    [super setSelected:selected];
    
    if (self.selected) {
        self.backgroundColor = self.tagSelectedColor;
    

    }else{
        self.backgroundColor = self.tagColor;

    }
    
    
}



#pragma mark - getter

- (UILabel *)tagLabel
{
    if (!_tagLabel) {
        UILabel * theView = [[UILabel alloc] init];
        [self addSubview:theView];
        _tagLabel = theView;
    }
    return _tagLabel;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
